package com.example.yamba;

import winterwell.jtwitter.Twitter;
import winterwell.jtwitter.TwitterException;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class StatusActivity extends Activity  implements OnClickListener, TextWatcher 
{

	
	  private static final String TAG = "StatusActivity";
	  EditText editText;
	  Button updateButton;
	  Twitter twitter;
	  TextView textCount; // <2>
	  SharedPreferences prefs;
	  
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.status);

        // Find views
        editText = (EditText) findViewById(R.id.editText);
        updateButton = (Button) findViewById(R.id.buttonUpdate);
        updateButton.setOnClickListener(this);

        textCount = (TextView) findViewById(R.id.textCount); // <3>
        textCount.setText(Integer.toString(140)); // <4>
        textCount.setTextColor(Color.GREEN); // <5>
        editText.addTextChangedListener(this); // <6>

      //  twitter = new Twitter("student", "password");
      //  twitter.setAPIRootUrl("http://yamba.marakana.com/api");
        
        prefs = PreferenceManager.getDefaultSharedPreferences(this);

     // Display the fragment as the main content.
        
    }
    
    public void onClick(View v) {
        String status = editText.getText().toString();
        new PostToTwitter().execute(status);
        Log.d(TAG, "onClicked");
      }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.status, menu);
        return true;
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
		switch(item.getItemId())
		{
		case R.id.menu_settings:
			startActivity(new Intent(this,PrefsActivity.class));
			break;
		case R.id.app_pref:
			getFragmentManager().beginTransaction().replace(android.R.id.content,
	                new PrefsFragment()).commit();
			break;	
		case R.id.itemServiceStart:
			startService(new Intent(this,UpdaterService.class));
			break;	
		case R.id.itemServiceStop:
			stopService(new Intent(this,UpdaterService.class));
			break;			
		}
		
		return true; //super.onOptionsItemSelected(item);
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onOptionsMenuClosed(menu);
		Toast.makeText(this, "menu closing", Toast.LENGTH_SHORT).show();
	}
	
  // TextWatcher methods
  public void afterTextChanged(Editable statusText) { // <7>
    int count = 140 - statusText.length(); // <8>
    textCount.setText(Integer.toString(count));
    textCount.setTextColor(Color.GREEN); // <9>
    if (count < 10)
      textCount.setTextColor(Color.YELLOW);
    if (count < 0)
      textCount.setTextColor(Color.RED);
  }
  
  public void beforeTextChanged(CharSequence s, int start, int count, int after) { // <10>
  }

  public void onTextChanged(CharSequence s, int start, int before, int count) { // <11>
  }
  
  // Asynchronously posts to twitter
  class PostToTwitter extends AsyncTask<String, Integer, String> {
    // Called to initiate the background activity
    @Override
    protected String doInBackground(String... statuses) {
      try {
        Twitter.Status status = ((YambaApplication1)getApplication()).getTwitter().updateStatus(statuses[0]);
        return status.text;
      } catch (TwitterException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
        return "Failed to post";
      }
    }

    // Called when there's a status to be updated
    @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      // Not used in this case
    }

    // Called once the background activity has completed
    @Override
    protected void onPostExecute(String result) {
      Toast.makeText(StatusActivity.this, "AMP 2- " + result, Toast.LENGTH_LONG).show();
    }
  }


	

	
}


